const { getDateDiff } = require('../../utils/utils')
const { fetch, newFetch } = require('../../utils/fetch')

// components/hotitem/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        data: Object,
        isEdit: Boolean

    },

    /**
     * 组件的初始数据
     */
    data: {
        time: ''
    },

    /**
     * 组件的方法列表
     */
    methods: {
        call(e) {
            wx.makePhoneCall({
                phoneNumber: this.data.data.pushPhone
            })
        },
        jump(e) {
            var typeid = e.currentTarget.dataset.id;
            if (!wx.getStorageSync('userInfo')) {
                wx.reLaunch({
                    url: '/pages/login/index'
                })
                return;
            }
            if(this.data.isEdit) {
                if(wx.getStorageSync('specificarea')) {
                    this.setData({
                      specificarea:wx.getStorageSync('specificarea')
                    })
                  }
                fetch(`wx/message/${this.data.specificarea}/detail`, 'POST', {
                    id:typeid
                }).then(res=>{
                    var details = encodeURIComponent(res.details);
                    var edittitle = encodeURIComponent(res.title);

                    // var str;
                    // str = res.details.replace(/\ +/g, "");//去掉空格
                    //  str = res.details.replace(/[\r\n]/g, "");//去掉回车换行
                    // res.details = str
                    delete res['details']
                    delete res['title']
                    if(res){
                        // 顺风车
                        if(res.typeone.title=='顺风车' || (res.typetwo && res.typetwo.title=='车找人')) {
                            wx.navigateTo({
                                url: '/pages/publishsfcar/index?ediddata='+JSON.stringify(res)+'&editid='+typeid+'&details='+details+'&edittitle='+edittitle
                            })
                            return;
                        } 
                        getApp().globalData.ediddata=res;
                        wx.navigateTo({
                            url: '/pages/publishlist/index?ediddata='+JSON.stringify(res)+'&editid='+typeid+'&details='+details+'&edittitle='+edittitle
                        })
                    }
                })
            } else {
                wx.navigateTo({
                    url: '/pages/indexDetails/index?id='+ e.currentTarget.dataset['id'],
                    success: function(res) {
                        // 通过eventChannel向被打开页面传送数据
                    }
                })
            }
        },
    },
    attached: function() {
        if(!this.data.data) {
            return;
        }
        let time = this.data.data.createDate && getDateDiff(this.data.data.createDate)
        this.setData({
            time
        })
    }
})
